<?php

include_once 'qc_base.php';
require_once('bootstrap.php');
require_once('lib/spyc/spyc.php');





// Perhaps this is for main models generation?
//Doctrine::generateModelsFromDb('models', array('maindb'), array('generateTableClasses' => true));

//Doctrine::generateYamlFromModels('schema.yml', 'models');
echo "<hr />";
//echo $manager->getCurrentConnection()->getName();

//$yaml = new sfYamlParser();
//$value = $yaml->parse(file_get_contents('schema.yml'));

//echo "<pre>";
//print_r($value);
//echo "</pre>";


echo "<p> Now, get info on the new database </p>";


$engine = 'mysql';
$db = 'sampledb';
$host = '127.0.0.1';
$user = 'root';
$port = '3306';

$dsn2 = $engine . ':' . '//' . $user . ':' . $password . '@' . $host . ':' . $port . '/' . $db;

$subdb = Doctrine_Manager::connection($dsn2,'subdb');



// 2010-08-15 comment out
//Doctrine::loadModels('subdbmodels');
//$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);


echo "<hr />";

$manager->setCurrentConnection('subdb');
echo $manager->getCurrentConnection()->getName();
echo "<hr / >";
$subdb->close();
$subdb->connect();
if($subdb->isConnected()){
	echo "CONNECTED!!!!";
}


//echo "<pre>";
//print_r($subdb);
//echo "</pre>";

// 2010-08-15 comment out
//Doctrine::generateModelsFromDb('subdbmodels', array('subdb'), array('generateTableClasses' => true));

//Doctrine::generateYamlFromModels('subdbschema.yml', 'subdbmodels');
Doctrine::generateYamlFromDb('subdbschema.yml', array('subdb'));


//$newyaml = new sfYamlParser();
//$ar_subdb_info = $newyaml->parse(file_get_contents('subdbschema.yml'));

$ar_subdb_info = spyc_load_file('subdbschema.yml');


echo "<pre>";
print_r($ar_subdb_info);
echo "</pre>";

$manager->setCurrentConnection('maindb');

//Doctrine::loadModels(QC_SYS_BASEDIR . '/models');

echo "<table>";
foreach($ar_subdb_info AS $table_name => $ar_table_info){
	echo "<tr><td>$table_name</td>";
	echo "<td><table>";
	$q_isthere = Doctrine_Query::create()
	->from('Tablesources ts') 
	->where('table_name = ?', $table_name)
	->andWhere('project_id = ?', 888);
	$doit = false;
	if($q_isthere->count() == 0){
	
		$newtable = new Tablesources();
		$newtable->table_name = $table_name;
		$newtable->project_id = 888;
		$newtable->table_alias = $table_name;
		$newtable->table_filter = 1;
		$newtable->save();
		$owner_table_id = $newtable->tablesource_id;
		$doit = true;
	}
	foreach($ar_table_info['columns'] AS $column_name => $ar_column_info){
		if($doit){
			$newvariable = new Variables();
			$newvariable->variable_name = $column_name;
			$newvariable->type = $ar_column_info['type'];
			if($ar_column_info['primary'] != 1){
				$is_key = 0;
			}else{
				$is_key = 1;
			}
			$newvariable->is_key = $is_key;
			$newvariable->source_id = $owner_table_id;
			$newvariable->def_show_text = '';
			$newvariable->save();
		}
		echo "<tr><td>$column_name</td><td></td><td>{$ar_column_info['type']}</td><td>{$ar_column_info['primary']}</td><td>{$ar_column_info['autoincrement']}</td></tr>";
	}
	echo "</table></td></tr>";
}
echo "</table>";


$q = Doctrine_Query::create()
->from('Crudsets cs');
//echo $q->getSqlQuery();

$Crudsets = $q->execute();
$ar_crudsets = $q->fetchArray();
foreach($Crudsets as $thisset){
	echo $thisset->cs_name;
}
echo "<pre>";
print_r($ar_crudsets);
echo "</pre>";
$newset = new Crudsets();
$newset->cs_name = 'MyNewTest';
$newset->save();

?>
